<?php

namespace App\Http\Controllers;

use App\Models\InviteOneOnOneShare;
use App\Models\InviteOneOnOneAcceptance;
use App\Facades\KafkaBridge;
use App\Exceptions\InviteOneOnOneShareNotFoundException;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

class InviteOneOnOneController extends BaseController
{
    //

    public function retrieve(Request $request)
    {
        $studentId = intval($request->header("X-Authenticated-Userid"));

        /**
         * @var InviteOneOnOneShare $share
         */
        $share = InviteOneOnOneShare::where('student_id', $studentId)->first();

        if (empty($share)) {
            throw new InviteOneOnOneShareNotFoundException();
        }

        if ($share->checkShareIsExpired()) {
            return new JsonResponse([
                'studentId' => $studentId,
                'status' => InviteOneOnOneShare::STATUS_EXPIRED,
                'left' => 0,
                'total' => InviteOneOnOneShare::TOTAL_NUM,
                'code' => $share->getShareCode(),
            ]);
        }

        $leftNum = InviteOneOnOneShare::TOTAL_NUM - $share->getAcceptNum();
        if ($leftNum <= 0) {
            $share->setStatus(InviteOneOnOneShare::STATUS_DONE);
            $share->save();
            $leftNum = 0;
        }

        return new JsonResponse([
            'studentId' => $studentId,
            'status' => $share->getStatus(),
            'left' => $leftNum,
            'total' => InviteOneOnOneShare::TOTAL_NUM,
            'code' => $share->getShareCode(),
        ]);
    }

    public function retrieveForPublic(Request $request)
    {
        $this->validate($request, [
            'code' => 'required|string'
        ]);
        $shareCode = $request->input('code');
        /**
         * @var InviteOneOnOneShare $share
         */
        $share = InviteOneOnOneShare::where('share_code', $shareCode)->first();

        if (empty($share)) {
            throw new InviteOneOnOneShareNotFoundException();
        }

        if ($share->checkShareIsExpired()) {
            return new JsonResponse([
                'studentId' => $share->getStudentId(),
                'status' => InviteOneOnOneShare::STATUS_EXPIRED,
                'left' => 0,
                'total' => InviteOneOnOneShare::TOTAL_NUM,
            ]);
        }

        $leftNum = InviteOneOnOneShare::TOTAL_NUM - $share->getAcceptNum();
        if ($leftNum <= 0) {
            $share->setStatus(InviteOneOnOneShare::STATUS_DONE);
            $share->save();
            $leftNum = 0;
        }

        return new JsonResponse([
            'studentId' => $share->getStudentId(),
            'status' => $share->getStatus(),
            'left' => $leftNum,
            'total' => InviteOneOnOneShare::TOTAL_NUM,
        ]);
    }

    public function retrieveByShareCode(Request $request)
    {
        $this->validate($request, [
            'code' => 'required|string'
        ]);
        $shareCode = $request->input('code');
        $studentId = intval($request->header("X-Authenticated-Userid"));
        /**
         * @var InviteOneOnOneShare $share
         */
        $share = InviteOneOnOneShare::where('share_code', $shareCode)->first();

        if (empty($share)) {
            throw new InviteOneOnOneShareNotFoundException();
        }

        if ($share->checkShareIsExpired()) {
            return new JsonResponse([
                'studentId' => $share->getStudentId(),
                'status' => InviteOneOnOneShare::STATUS_EXPIRED,
                'left' => 0,
                'total' => InviteOneOnOneShare::TOTAL_NUM,
            ]);
        }

        $leftNum = InviteOneOnOneShare::TOTAL_NUM - $share->getAcceptNum();
        if ($leftNum <= 0) {
            $share->setStatus(InviteOneOnOneShare::STATUS_DONE);
            $share->save();
            $leftNum = 0;
        }

        $studentAcceptance = InviteOneOnOneAcceptance::where('student_id', $studentId)->first();
        if (!empty($studentAcceptance)) {
            $share->setStatus(InviteOneOnOneShare::STATUS_ACCEPTED);
        }

        return new JsonResponse([
            'studentId' => $share->getStudentId(),
            'status' => $share->getStatus(),
            'left' => $leftNum,
            'total' => InviteOneOnOneShare::TOTAL_NUM,
        ]);
    }
}
